package org.edu.modules.stu.common.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.edu.common.util.PasswordUtil;
import org.edu.common.util.oConvertUtils;
import org.edu.modules.stu.common.entity.VeBaseAppUser;
import org.edu.modules.stu.common.mapper.VeBaseAppUserMapper;
import org.edu.modules.stu.common.service.VeBaseAppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class VeBaseAppUserServiceImpl extends ServiceImpl<VeBaseAppUserMapper, VeBaseAppUser> implements VeBaseAppUserService {

    @Autowired
    private VeBaseAppUserMapper appUserMapper;

    @Override
    public VeBaseAppUser getVeBaseAppUserById(String id) {
        return appUserMapper.getVeBaseAppUserById(id);
    }

    @Override
    public List<Map<String, Object>> getBaseSysRoleList(String userId) {
        return appUserMapper.getBaseSysRoleList(userId);
    }
    @Override
    public Map getSysRoleUser(String userId, Integer roleId) {

        return appUserMapper.getSysRoleUser(userId, roleId);
    }

    @Override
    public int updateAppUserByUserId(String userId) {
        return appUserMapper.updateAppUserByUserId(userId);
    }

    @Override
    public int updateSysUserDel(String userId) {
        return appUserMapper.updateSysUserDel(userId);
    }
    @Override
    public int updateSysUserDelBatch(List<String> userIds) {
        return appUserMapper.updateSysUserDelBatch(userIds);
    }

    @Override
    public int deleteOrgan(String userId) {
        return appUserMapper.deleteOrgan(userId);
    }

    @Override
    public int deleteRole(String userId) {
        return appUserMapper.deleteRole(userId);
    }
    @Override
    public void deleteAppUser(String userId) {

            this.updateAppUserByUserId(userId);

            this.updateSysUserDel(userId);

            this.deleteOrgan(userId);

            this.deleteRole(userId);

    }

    @Override
    public int addSysRoleUser(String userId, Integer roleId) {

        return appUserMapper.addSysRoleUser(userId, roleId);
    }
    @Override
    public Integer getIdByRoleCode(String code) {

        return appUserMapper.getIdByRoleCode(code);
    }
    @Override
    public int addSysUser(VeBaseAppUser veBaseAppUser) {

        return appUserMapper.addSysUser(veBaseAppUser);
    }
    @Override
    public VeBaseAppUser getPwdAndSalt(VeBaseAppUser veBaseAppUser) {
        String salt = oConvertUtils.randomGen(8);
        String passwordEncode = PasswordUtil.encrypt(veBaseAppUser.getUserName(), veBaseAppUser.getUserPassword(), salt);
        veBaseAppUser.setUserPassword(passwordEncode);
        veBaseAppUser.setSalt(salt);
        return veBaseAppUser;
    }
    @Override
    public String pwdVerify(String pwd) {
        int flag = 0;
        if (pwd.matches(".*[0-9].*")) {
            flag++;
        }
        if (pwd.matches(".*[a-z].*")) {
            flag++;
        }
        if (pwd.matches(".*[A-Z].*")) {
            flag++;
        }
        if (pwd.matches(".*[!@*#$\\-_()+=&￥].*")) {
            flag++;
        }
        if (flag == 1) {
            return "弱";
        }
        if (flag == 2) {
            return "中";
        }
        return "强";
    }

}
